//
//  FCoinVC.m
//  AppNew
//
//  Created by 华伟 on 2019/4/4.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "FCoinVC.h"
#import "SaleVC.h"
@interface FCoinVC ()<UINavigationControllerDelegate>

@property(nonatomic, strong)UIView *saleBuyWindowView;

@end

@implementation FCoinVC

- (void)hideMask{
    __weak __typeof(self) weakSelf = self;
    self.saleBuyWindowView.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.saleBuyWindowView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
    }completion:^(BOOL finished) {
    }];
}
- (void)salePush{
     __weak __typeof(self) weakSelf = self;
     self.saleBuyWindowView.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.saleBuyWindowView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
    }completion:^(BOOL finished) {
        
        SaleVC *vc = [SaleVC new];
        vc.hidesBottomBarWhenPushed = true;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)buyPush{
    
}
- (IBAction)relaseAction:(UIButton *)sender {//发布按钮点击事件
    __weak __typeof(self) weakSelf = self;
    self.saleBuyWindowView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.saleBuyWindowView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    }completion:^(BOOL finished) {
       
//
//        SaleVC *vc = [SaleVC new];
//        [self.navigationController pushViewController:vc animated:YES];
    }];
    
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isPushSelfClass = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isPushSelfClass animated:animated];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.delegate = self;
    _selectStr = @"CNY";
    _saleState = @"buy";
    _buyDataArr = [NSMutableArray array];
    _sellDataArr = [NSMutableArray array];
    [self initViews];
    
}

- (void)initViews{
    
    _realeseButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _realeseButton.layer.borderWidth = 1;
    
    _selectButton = [[UIButton alloc] initWithFrame:CGRectMake(_rmbButton.t_left + _rmbButton.t_width / 4, _topView.t_height - 2, _rmbButton.t_width / 2, 2)];
    _selectButton.backgroundColor = [UIColor whiteColor];
    [_topView addSubview:_selectButton];
    
    [_rmbButton addTarget:self
                   action:@selector(moneyAction:)
         forControlEvents:UIControlEventTouchUpInside];
    [_rbButton addTarget:self
                   action:@selector(moneyAction:)
         forControlEvents:UIControlEventTouchUpInside];
    [_gbButton addTarget:self
                   action:@selector(moneyAction:)
         forControlEvents:UIControlEventTouchUpInside];
    [_myButton addTarget:self
                   action:@selector(moneyAction:)
         forControlEvents:UIControlEventTouchUpInside];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:[NSBundle mainBundle]]
     forCellReuseIdentifier:@"TableView_Cell"];
    
    UIView *maskViewAd = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight)];
    [maskViewAd addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideMask)] ];
    maskViewAd.backgroundColor = [UIColor clearColor];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 250, ScreenWidth, 250)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [maskViewAd addSubview:bottomView];
    
    UIView *lView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 8, 15)];
    lView.backgroundColor = BaseColor;
    [bottomView addSubview:lView];
    
    UILabel *btitle = [[UILabel alloc] initWithFrame:CGRectMake(lView.t_right+10, 10, 100, 15)];
    btitle.text = @"发布交易订单";
    btitle.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:btitle];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, lView.t_bottom+10, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorFromHexRGB:@"e0e0e0"];
    [bottomView addSubview:lineView];
    
    UIView *saleView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth/2/2-40, 70, 80, 80)];
    saleView.backgroundColor = [UIColor colorWithRed:28/255.0 green:172/255.0 blue:145/255.0 alpha:1];
    
    UILabel *saleTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 50, 50)];
    saleTitle.text = @"出售";
    saleTitle.textAlignment = NSTextAlignmentCenter;
    saleTitle.font = [UIFont systemFontOfSize:22];
    saleTitle.textColor = [UIColor whiteColor];
    [saleView addSubview:saleTitle];
    [saleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(salePush)]];
    [bottomView addSubview:saleView];
    
    UILabel *saleTitleText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth/2, 20)];
    saleTitleText.text = @"在线出售";
    saleTitleText.textAlignment = NSTextAlignmentCenter;
    saleTitleText.font = [UIFont systemFontOfSize:15];
    CGPoint poin = CGPointMake(saleView.center.x, saleView.center.y+ 65);
    saleTitleText.center = poin;
    [bottomView addSubview:saleTitleText];
    
    UIView *byView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth/2+40, 70, 80, 80)];
    byView.backgroundColor = [UIColor colorWithRed:238/255.0 green:90/255.0 blue:101/255.0 alpha:1];
    UILabel *byTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 50, 50)];
    byTitle.text = @"购买";
    byTitle.textAlignment = NSTextAlignmentCenter;
    byTitle.font = [UIFont systemFontOfSize:22];
    byTitle.textColor = [UIColor whiteColor];
    [byView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buyPush)]];
    [byView addSubview:byTitle];
    [bottomView addSubview:byView];
    
    UILabel *buyTitleText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth/2, 20)];
    buyTitleText.text = @"在线购买";
    buyTitleText.textAlignment = NSTextAlignmentCenter;
    buyTitleText.font = [UIFont systemFontOfSize:15];
    CGPoint poin1 = CGPointMake(byView.center.x, byView.center.y+ 65);
    buyTitleText.center = poin1;
    [bottomView addSubview:buyTitleText];
    
    
    UIWindow *mainWindow = [[UIApplication sharedApplication].delegate window];
    [mainWindow addSubview:maskViewAd];

    self.saleBuyWindowView = maskViewAd;
}

- (void)moneyAction:(UIButton *)button {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame = self->_selectButton.frame;
        frame.origin.x = button.frame.origin.x + button.frame.size.width / 4;
        self->_selectButton.frame = frame;
        
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self requestData];
    
}

- (void)requestData{
    
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/trade/tradeInfo?hb=%@&page=1",_selectStr];
    [NetRequestTool get:url
                 params:nil
                success:^(id  _Nonnull json) {
                    
                    BOOL flag = [[json objectForKey:@"flag"] boolValue];
                    if (flag) {
                        
                        NSDictionary *dic = [json objectForKey:@"data"];
                        NSDictionary *buyDic = [dic objectForKey:@"buyList"];
                        NSDictionary *sellDic = [dic objectForKey:@"sellList"];
                        self->_buyDataArr = [buyDic  objectForKey:@"list"];
                        self->_sellDataArr = [sellDic objectForKey:@"list"];
                        [self->_tableView reloadData];
                        
                    }else{
                        
                        [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                        
                    }
                    
                } failure:^(NSError * _Nonnull error) {
                    
                }];
    
    
}

#pragma mark - UITableViewDelegate,UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([_saleState isEqualToString:@"buy"]) {
        return _buyDataArr.count;
    }else if ([_saleState isEqualToString:@"sale"]) {
        return _sellDataArr.count;
    }else{
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableView_Cell"];
    cell.tradButton.tag = indexPath.row + 100;
    if (!cell) {
        cell = [[TableViewCell alloc] init];
    }
    NSDictionary *dic = [NSDictionary dictionary];
    if ([_saleState isEqualToString:@"buy"]) {
        dic = _buyDataArr[indexPath.row];
        cell.tradButton.backgroundColor = [UIColor colorFromHexRGB:@"4EAB92"];
        [cell.tradButton setTitle:@"买入" forState:UIControlStateNormal];
    }else if ([_saleState isEqualToString:@"sale"]) {
        dic = _sellDataArr[indexPath.row];
        cell.tradButton.backgroundColor = [UIColor colorFromHexRGB:@"DD6366"];
        [cell.tradButton setTitle:@"卖出" forState:UIControlStateNormal];
    }else{

    }
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userNameTb"]];
    cell.currentPrice.text = [NSString stringWithFormat:@"%@%@/%@",[dic objectForKey:@"price"],_selectStr,[dic objectForKey:@"quota"]];
    cell.totalPrice.text = [NSString stringWithFormat:@"总金额%@",[dic objectForKey:@"zjg"]];
    [cell.tradButton addTarget:self
                        action:@selector(tradAction:)
              forControlEvents: UIControlEventTouchUpInside];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 62;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

- (void)tradAction:(UIButton *)button{
    
    NSDictionary *dic = [NSDictionary dictionary];
    if ([_saleState isEqualToString:@"buy"]) {
            dic = _buyDataArr[button.tag - 100];
    }else{
            dic = _sellDataArr[button.tag - 100];
    }

    ENsureTradView *etView = [[ENsureTradView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    etView.backgroundColor = [UIColor clearColor];
    etView.dataDic = dic;
    if ([_saleState isEqualToString:@"buy"]) {
        etView.isSell = @"1";
        [etView.ensureButton setTitle:@"确认购买" forState:UIControlStateNormal];
        etView.ensureButton.backgroundColor = [UIColor colorFromHexRGB:@"3961F6"];
    }else{
        etView.isSell = @"2";
        
        [etView.ensureButton setTitle:@"确认出售" forState:UIControlStateNormal];
        etView.ensureButton.backgroundColor = [UIColor colorFromHexRGB:@"DD6366"];
    }
    [self.view addSubview:etView];
    
}

- (void)sellAction2{
    
    ReleaseController *rvc = [[ReleaseController alloc] init];
    rvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rvc animated:YES];
    
}

- (void)buyAction2{
    
    
    
}

- (IBAction)buttonAction:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    NSArray *titleArr = @[@"CNY",@"JPY",@"USD",@"HKD"];
    _selectStr = titleArr[button.tag - 1];
    [self requestData];
    
}

- (IBAction)bottomBuyAction:(id)sender {
    
    _saleState = @"buy";
    [_tableView reloadData];
    
}

- (IBAction)bottomSaleAction:(id)sender {
    
    _saleState = @"sale";
    [_tableView reloadData];
    
}

- (IBAction)releaseButtonAction:(id)sender {
    
    ReleaseView *rView = [[ReleaseView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    rView.backgroundColor = [UIColor clearColor];
    [rView.sellButton addTarget:self
                         action:@selector(sellAction2)
               forControlEvents:UIControlEventTouchUpInside];
    
    [rView. buyButton addTarget:self
                         action:@selector(buyAction2)
               forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rView];
    
}

@end
